package viper.main;

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
/**
*
* @author Ji Kim
* 
* Revision number: 1
* Date created: 04/06/10
* Date of Last Back-up on SVN: none
* 
* <p>Plays Ringtone when entering a call
*/
public class RingtonePlayer implements Runnable {
	
	private Clip clip;
	private String location;
	private AudioInputStream ais;
	private boolean running;
	
	//uses default ringtone
	public RingtonePlayer(){
		running = true;
		location = "audiofiles/ring1.wav"; //specify file location of ringtone in .wav form
		//create clip object
		try{
			clip = AudioSystem.getClip();
		}catch(LineUnavailableException lue){
			System.out.println("LUE 1");
		}
		//create file from ringtone.wav
		File soundFile = new File(location);
		try{
			//make stream from audio file
			ais = AudioSystem.getAudioInputStream(soundFile);
		}catch(UnsupportedAudioFileException uafe){
			System.out.println("UAFE");
		}catch(IOException ioe){
			System.out.println("IOE 1");
		}
		try{
			clip.open(ais);
		}catch(LineUnavailableException lue){
			System.out.println("LUE 2");
		}catch(IOException ioe){
			System.out.println("IOE 2");
		}
	}
	
	//overloaded to provide custom location for ringtone
	public RingtonePlayer(String location){
		running = true;
		this.location = location;
		try{
			clip = AudioSystem.getClip();
		}catch(LineUnavailableException lue){
			System.out.println("LUE 1");
		}
		File soundFile = new File(location);
		try{
			ais = AudioSystem.getAudioInputStream(soundFile);
		}catch(UnsupportedAudioFileException uafe){
			System.out.println("UAFE");
		}catch(IOException ioe){
			System.out.println("IOE 1");
		}
		try{
			clip.open(ais);
		}catch(LineUnavailableException lue){
			System.out.println("LUE 2");
		}catch(IOException ioe){
			System.out.println("IOE 2");
		}
	}
	
	public void end(){
		running = false;
	}

	public void run(){		
		clip.loop(Clip.LOOP_CONTINUOUSLY);
		while(running){}
		clip.stop();
	}

}
